package middlewares

import (
	"awesomeProject/goods-web/models"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"net/http"
)

// IsAdminAuth 进行用户管理员身份的验证
func IsAdminAuth() gin.HandlerFunc {
	return func(c *gin.Context) {
		claims, _ := c.Get("claims")
		curUser := claims.(*models.CustomClaims)

		zap.S().Infof("当前的用户[%s] 权限: %d", curUser.NickName, curUser.AuthorityId)

		if curUser.AuthorityId != 2 { // == 2表示admin管理员，才能够进行查询用户列表
			c.JSON(http.StatusForbidden, gin.H{
				"msg": "当前用户无权限",
			})
			c.Abort()
			return
		}
		c.Next()
	}
}
